package com.example.springbootpetstoredemo.Controller;

import com.alibaba.fastjson.JSON;
import com.example.springbootpetstoredemo.Pojo.IndexInfo;
import com.example.springbootpetstoredemo.Service.IndexService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Controller
public class IndexController {

    @Autowired
    IndexService indexService;

    @RequestMapping("/index")
    public String Index(HttpServletRequest request, HttpServletResponse response){return "index";}

    @RequestMapping(value = "/GetIndexInfo")
    @ResponseBody
    public void GetIndexInfo(HttpServletRequest request,HttpServletResponse response) throws IOException {
        IndexInfo indexInfo = indexService.getIndexInfo();
        String json = JSON.toJSONString(indexInfo);
        response.setContentType("text/Json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().print(json);
    }

    @RequestMapping("/updateIndexStoreCard")
    @ResponseBody
    public void updateIndexStoreCard(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String newName = request.getParameter("newName");
        String newInfo = request.getParameter("newInfo");
        String new_description = request.getParameter("new_description");
        String oldnName = request.getParameter("oldName");
        indexService.updateIndexStoreCard(newName,newInfo,new_description,oldnName);
        response.getWriter().print("success");
    }

    @RequestMapping("/updateIndexStoreInformation")
    @ResponseBody
    public void updateIndexStoreInformation(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String managerInfo = request.getParameter("managerInfo");
        String locationInfo = request.getParameter("locationInfo");
        String phoneInfo = request.getParameter("phoneInfo");
        String emailInfo = request.getParameter("emailInfo");
        String validateInfo = request.getParameter("validateInfo");
        String storeName = request.getParameter("storeName");
        indexService.updateIndexStoreInformation(managerInfo,locationInfo,phoneInfo,emailInfo,validateInfo,storeName);
        response.getWriter().print("success");
    }

    @RequestMapping("addIndexImage")
    public void addIndexImage(@RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取文件名和保存路径
        String fileName = file.getOriginalFilename();
        String uploadDir = "src/main/resources/static/image/index/";
        String uploadDir2 = "target/classes/static/image/index/";
        //将文件保存到指定目录下
        try {
            byte[] bytes = file.getBytes();
            Path path1 = Paths.get(uploadDir + fileName);
            Path path2 = Paths.get(uploadDir2 + fileName);
            Files.write(path1,bytes);
            Files.write(path2,bytes);
        }catch (IOException e){
            e.printStackTrace();
        }
        response.getWriter().print("success");
    }
}
